import hex_md5 from './md5.js';
import toutf from './base64.js';
let env = 'noprd';
let thirdUrl, requestUrl, key;
if (env == 'prd') {
  thirdUrl = 'https://api.winhc.cn/miniapp/';
  requestUrl = 'https://mis.winhc.cn/';
  key = 'iroolf958i09d0lg69fg';
} else {
  thirdUrl = 'https://t.winhc.net/miniapp/';//http://106.14.81.247:9094/
  // requestUrl = 'http://10.1.10.207:8790/';// http://10.1.10.170:8790/ https://tmis.winhc.net/
  requestUrl = 'https://tmis.winhc.net/'
  // requestUrl = 'http://10.1.10.182:8790/'
  key = '123456';
}

let showTipMsg = (msg) => {
  wx.showModal({
    title: '提示',
    content: msg,
    showCancel: false,
    confirmText: "知道了",
    success: function (res) {
      if (res.confirm) {
      } else if (res.cancel) {
      }
    }
  })
}

const appid = "wxc8a471bb1cf4c4a0";

const signType = "MD5";
const request_api = (res, fnScu, fnFail) => {
  wx.request({
    url: thirdUrl + 'apsara/sessionKey', //仅为示例，并非真实的接口地址
    method: 'GET',
    data: {
      code: res
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (obj) {
      fnScu(obj);
    },
    fail: function (obj) {
      fnFail(obj);
    }
  })
}


//调用mis接口Post
const requestPostMisApi = (serviceName, bodyInfo, fnScu, fnFail) => {
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址https://tmis.winhc.net/token
    method: 'POST',
    data: bodyInfo,
    header: {
      'content-type': 'application/json;charset=UTF-8' // 默认值
    },
    success: function (obj) {
      // fnScu(obj);
      if (obj.statusCode == 200) {
        fnScu(obj);
      } else if (obj.statusCode == 400) {
        showTipMsg("格式错误");
        fnFail()
      } else if (obj.statusCode == 401) {
        fnFail()// 未授权（登陆过程为用户名或者密码错误）
      } else if (obj.statusCode == 403) {
        showTipMsg("权限不足");
        fnFail()//权限不足
      } else if (obj.statusCode == 500) {
        showTipMsg("系统繁忙");
        fnFail()//系统繁忙
      } else {
        fnFail()
      }
    },
    fail: function (obj) {
      fnFail(obj)
    }
  })
}

//调用mis接口put
const requestPutMisApi = (serviceName, bodyInfo, fnScu, fnFail) => {
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址https://tmis.winhc.net/token
    method: 'PUT',
    data: bodyInfo,
    header: {
      'content-type': 'application/json;charset=UTF-8' // 默认值
    },
    success: function (obj) {
      // fnScu(obj);
      if (obj.statusCode == 200) {
        fnScu(obj);
      } else if (obj.statusCode == 400) {
        showTipMsg("格式错误");
        fnFail()
      } else if (obj.statusCode == 401) {
        fnFail()// 未授权（登陆过程为用户名或者密码错误）
      } else if (obj.statusCode == 403) {
        showTipMsg("权限不足");
        fnFail()//权限不足
      } else if (obj.statusCode == 500) {
        showTipMsg("系统繁忙");
        fnFail()//系统繁忙
      } else {
        fnFail()
      }
    },
    fail: function (obj) {
      fnFail(obj)
    }
  })
}

//调用mis queryGetApi
const requestGetMisApi = (serviceName, token, bodyInfo, fnScu, fnFail) => {
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址
    method: 'GET',
    data: bodyInfo,
    header: {
      'content-type': 'application/json;charset=UTF-8', // 默认值
      "Auth": "Bearer" + token
    },
    success: function (obj) {
      // fnScu(obj);
      if (obj.statusCode == 200) {
        fnScu(obj);
      } else if (obj.statusCode == 400) {
        showTipMsg("格式错误");
        fnFail()
      } else if (obj.statusCode == 401) {
        fnFail()// 未授权（登陆过程为用户名或者密码错误）
      } else if (obj.statusCode == 403) {
        showTipMsg("权限不足");
        fnFail()//权限不足
      } else if (obj.statusCode == 500) {
        showTipMsg("系统繁忙");
        fnFail()//系统繁忙
      } else {
        fnFail()
      }
    },
    fail: function (obj) {
      fnFail(obj)
    }
  })
}
//调用mis queryGetApi
const requestMisApi = (method, serviceName, bodyInfo, fnScu, fnFail) => {
  console.log(method+' :', serviceName+' =', bodyInfo)
  wx.request({
    url: requestUrl + serviceName, //仅为示例，并非真实的接口地址
    method: method,
    data: bodyInfo,
    header: {
      'content-type': 'application/json;charset=UTF-8', // 默认值
      "Auth": "Bearer" + wx.getStorageSync("token")
    },
    success: function (obj) {
      console.log(obj)
      if (obj.statusCode == 200) {
        fnScu(obj.data);
      } else if (obj.statusCode == 400) {
        showTipMsg("格式错误");
        fnFail()
      } else if (obj.statusCode == 401) {
        fnFail()// 未授权（登陆过程为用户名或者密码错误）
      } else if (obj.statusCode == 403) {
        showTipMsg("权限不足");
        fnFail()//权限不足
      } else if (obj.statusCode == 500) {
        showTipMsg("系统繁忙");
        fnFail()//系统繁忙
      } else {
        fnFail()
      }
    },
    fail: function (obj) {
      fnFail(obj.data)
    }
  })
}

const requestUploadImg = (serviceName, bodyInfo, fnScu, fnFail) => {
  wx.uploadFile({
    url: requestUrl + serviceName, //仅为示例，非真实的接口地址
    filePath: bodyInfo.path,
    name: 'file',
    formData: {
      surce: 'test',
      id: '12345'
    },
    success: function (obj) {
      fnScu(obj);
    },
    fail: function (obj) {
      fnFail(obj);
    }
  })
}

// queryApi: function(token){
//   console.log("token====", token)
//   wx.request({
//     url: 'https://tmis.winhc.net/projectBaseInfos', //仅为示例，并非真实的接口地址
//     method: 'GET',
//     data: {
//       body: {},
//       pageNum: '1',
//       pageSize: '10'
//     },
//     header: {
//       'content-type': 'application/json;charset=UTF-8', // 默认值
//       "Auth": "Bearer" + token
//     },
//     success: function (obj) {
//       console.log("调用mis查询成功=", obj)
//     },
//     fail: function (obj) {
//       console.log("调用mis查询失败=", obj)
//     }
//   })
// }

// //测试调用mis接口
// tesMisApi: function(){
//   let _this = this;
//   wx.request({
//     url: 'https://tmis.winhc.net/token', //仅为示例，并非真实的接口地址
//     method: 'POST',
//     data: {
//       loginId: 'hxm',
//       password: '111111'
//     },
//     header: {
//       'content-type': 'application/json;charset=UTF-8' // 默认值
//     },
//     success: function (obj) {
//       console.log("调用mis成功=", obj)
//       _this.queryApi(obj.data.token);
//     },
//     fail: function (obj) {
//       console.log("调用mis失败=",obj)
//     }
//   })
// },


module.exports = {
  request_api: request_api,
  requestPostMis_api: requestPostMisApi,
  requestPutMis_api: requestPutMisApi,
  requestGetMis_api: requestGetMisApi,
  requestMisApi: requestMisApi,
  requestUploadImg: requestUploadImg,
  request_url: thirdUrl
};